// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt
/* eslint-disable */

frappe.query_reports["SRM Sample RequestCaigou"] = {
	"filters": [
		{
			"fieldname": "docstatus",
			"fieldtype": "Select",
			"label": "状态",
			"options": ['', 'Save','Hand In'],
			"default": '',
			"width": 0
		},
		{
			"fieldname": "creation_from",
			"fieldtype": "Date",
			"label": "制单日期从",
			"default": frappe.datetime.add_days(frappe.datetime.get_today(), -100),
			"width": 0
		},
		{
			"fieldname": "creation_to",
			"fieldtype": "Date",
			"label": "制单日期到",
			"default": frappe.datetime.add_days(frappe.datetime.get_today(), 1),
			"width": 0
		},
	],

	onload: function(report) {
		report.page.add_inner_button(__("新增打样申请(采购)"), function() {
			frappe.new_doc("SRM Sample RequestCaigou");
		}).addClass('btn-primary');

		// report.page.add_inner_button(__("样品承认"), function() {
		// 	var selected_rows = [];
		// 	$('.dt-scrollable').find(":input[type=checkbox]").each((idx, row) => {
		// 		if(row.checked){
		// 			selected_rows.push(frappe.query_report.data[idx]);
		// 			if (selected_rows.length > 1) {
		// 				frappe.throw('只允许操作一行!')
		// 			}
		// 			else if (selected_rows.length = 1){
		// 				console.log("*** selected row id : " + idx);
		// 				frappe.new_doc("SRM Sample Admit");
		// 			}
		// 		}
		// 	});
			
		// }).addClass('btn-primary')
	},

	after_datatable_render: function(datatable_obj) {
		// 自动刷新一次
		setTimeout(() => frappe.query_report.refresh(), 6000000);
	},

	"formatter": function(value, row, column, data, default_formatter) {
		
		value = default_formatter(value, row, column, data);
		let format_fields = ["docstatus"];
		if (format_fields && data && data["docstatus"]) {
			
			var color_field = data["docstatus"];
			let color = undefined;	
			if (color_field == 1) {
				color = 'green';
			}
			if (color_field == 2) {
				color = 'red';
			}
			if (color){
				value =`<div style='margin:0px;padding-left:5px;color:${color}!important;'>${value}</div>`            
			}
		}

		return value;
	},

	get_datatable_options(options) {
        return Object.assign(options, {
            checkboxColumn: true,
			events: {
				onCheckRow(row) {
					console.log(row)
				},
			}
        });
    },
};
